package com.hlk.hlk_blog.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hlk.hlk_blog.entity.Type;
import com.hlk.hlk_blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class TypeController {
    @Autowired
    private TypeService typeService;

    @GetMapping("/types")
    public String types(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                        Model model) {
        PageHelper.startPage(pageNum, 5);
        List<Type> allType = typeService.getAllType();
        PageInfo<Type> pageInfo = new PageInfo<>(allType);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/types";
    }

    /**
     * 跳转type新增页面
     */
    @GetMapping("/types/input")
    public String toaddType(Model model) {
        model.addAttribute("type", new Type());
        return "admin/types-input";
    }

    /**
     * 跳转type编辑页面
     */
    @GetMapping("/types/{id}/input")
    public String toEditType(@PathVariable Long id, Model model) {
        Type type = typeService.getTypeById(id);
        model.addAttribute("type", type);
        return "admin/types-input";
    }

    /**
     * 新增
     */
    @PostMapping("/types")
    public String addType(Type type, RedirectAttributes attributes) {
        Type name = typeService.getTypeByname(type.getName());
        if (name != null) {
            attributes.addFlashAttribute("mssage", "不能添加重复的分类");
            return "redirect:/admin/types/input";
        } else {
            typeService.saveType(type);
            attributes.addFlashAttribute("mssage", "添加成功");
            //不能直接跳转到types页面，否则不会显示type数据(没经过types方法)
            return "redirect:/admin/types";
        }
    }

    /**
     * 编辑
     */
    @PostMapping("/types/{id}")
    public String editType(@PathVariable Long id, Model model, Type type,
                           RedirectAttributes attributes) {
        Type name = typeService.getTypeByname(type.getName());
        if (name != null) {
            attributes.addFlashAttribute("mssage", "修改失败，分类名称重复！");
            return "redirect:/admin/types/input";
        } else {
            typeService.updateType(type);
            attributes.addFlashAttribute("mssage", "修改成功");
            //不能直接跳转到types页面，否则不会显示type数据(没经过types方法)
            return "redirect:/admin/types";

        }
    }

    /**
     * 删除
     */
    @GetMapping("/types/{id}/delete")
    public String deleteType(@PathVariable Long id, RedirectAttributes attributes) {
        boolean b = typeService.delectType(id);
        if (b == true) {
            attributes.addFlashAttribute("mssage", "删除成功！");
            return "redirect:/admin/types";
        } else {
            attributes.addFlashAttribute("mssage", "删除失败！");
            return "redirect:/admin/types";
        }
    }

}
